package com.wuuxiang.polestar.core.observer;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import java.util.List;

/**********************************************************
 * 项目名称：polestar
 * 类描述：   
 * 创建人：夏鸿鹏 
 * 创建时间：2017-11-24 14:49   
 * 修改备注：   
 *
 **********************************************************/

public abstract class AbstractSubject implements ISubject {

    // 观察者列表
    private final List<IObserver> watchers = Lists.newArrayList();

    @Override
    public void register(final IObserver watcher) {
        watchers.add(Preconditions.checkNotNull(watcher));
    }

    @Override
    public void notify(final String key, final String value) {
        for (final IObserver watcher : watchers) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    watcher.notified(key, value);
                }
            }).start();
        }
    }
}
